/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

//import teori1.*;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author admin
 */
public class Perpustakaan {

    public static String NAMAPERPUSTAKAAN = "PERPUSTAKAAN KU";
    public static ArrayList<Publikasi> DAFTAR_PUBLIKASI = new ArrayList<Publikasi>();
    public static ArrayList<Anggota> DAFTAR_ANGGOTA = new ArrayList<Anggota>();

    private static TimerBacaPublikasi TIMER_BACA_PUBLIKASI;
    private static TimerSimpanPublikasi TIMER_SIMPAN_PUBLIKASI;

    public static void tambahPublikasi(Publikasi inPublikasi) {
        DAFTAR_PUBLIKASI.add(inPublikasi);
    }

    public static void tambahAnggota(Anggota inAnggota) {
        DAFTAR_ANGGOTA.add(inAnggota);
    }

    public static String bacaDaftarAnggota() {
        if (DAFTAR_ANGGOTA.isEmpty() == false) {
            String hasil = new String();
            for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
                hasil += DAFTAR_ANGGOTA.get(i).getNama();
            }
            return hasil;
        } else {
            return "kosong";
        }
    }

    public static String bacaDaftarPublikasi() {
        if (DAFTAR_PUBLIKASI.isEmpty() == false) {
            String hasil = new String();
            for (int i = 0; i < DAFTAR_PUBLIKASI.size(); i++) {
                hasil += DAFTAR_PUBLIKASI.get(i).getJudul() + "\n";
            }
            return hasil;
        } else {
            return "kosong";
        }

    }

    public static void simpanFilePublikasi() {
        FileOutputStream outStream = null;
        if (DAFTAR_PUBLIKASI.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Publikasi Kosong");
        } else {
            try {
                File outFile = new File("publikasi.dat");
                outStream = new FileOutputStream(outFile);
                ObjectOutputStream outObjectStream = new ObjectOutputStream(outStream);
                outObjectStream.writeObject(DAFTAR_PUBLIKASI);
                outStream.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void simpanFileAnggota(){
        FileOutputStream outStream = null;
        if (DAFTAR_ANGGOTA.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Anggota Kosong");
        } else {
            try {
                File outFile = new File("anggota.dat");
                outStream = new FileOutputStream(outFile);
                ObjectOutputStream outObjectStream = new ObjectOutputStream(outStream);
                outObjectStream.writeObject(DAFTAR_ANGGOTA);
                outStream.close();
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void bacaFilePublikasi() {
        try {
            FileInputStream inStream = null;
            File inFile = new File("publikasi.dat");
            inStream = new FileInputStream(inFile);
            ObjectInputStream inObjectStream = new ObjectInputStream(inStream);
            DAFTAR_PUBLIKASI = (ArrayList<Publikasi>) inObjectStream.readObject();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void bacaFileAnggota(){
        try {
            FileInputStream inputStream = null;
            File inFile = new File("anggota.dat");
            inputStream = new FileInputStream(inFile);
            ObjectInputStream inObjectStream = new ObjectInputStream(inputStream);
            DAFTAR_ANGGOTA = (ArrayList<Anggota>) inObjectStream.readObject();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void tambahTabelPublikasi(Publikasi publikasi) {
        try {
            String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:xe";
                String userid = "HR";
                String password = "rosi";
                Connection conn;
                OracleDataSource ds;
                ds = new OracleDataSource();
                ds.setURL(jdbcUrl);
                conn = ds.getConnection(userid, password);
                System.out.println("Koneksi is OK");
                Statement statement = conn.createStatement();
//                String sql1 = " insert into publikasi "
//                   + "values ('"+textKodePublikasi.getText()+"' , '"
//                   + textJudul.getText()+"' , '"
//                   + textPenerbit.getText()+"' , '"
//                   + textPenulis.getText()+"' , '"
//                   + Integer.parseInt(textTahun.getText())+"')";
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void simpanTabelPublikasi() {
        if (DAFTAR_PUBLIKASI.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Daftar Publikasi Kosong");
        } else {
            try {
                String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:xe";
                String userid = "HR";
                String password = "rosi";
                Connection conn;
                OracleDataSource ds;
                ds = new OracleDataSource();
                ds.setURL(jdbcUrl);
                conn = ds.getConnection(userid, password);
                System.out.println("Koneksi is OK");
                Statement statement = conn.createStatement();
                for (int i = 0; i < DAFTAR_PUBLIKASI.size(); i++) {
                    String sql = "insert into publikasi (id_koleksi,judul) values ('"
                            + DAFTAR_PUBLIKASI.get(i).getIdKoleksi()+"', '"
                            + DAFTAR_PUBLIKASI.get(i).getJudul() + ", "
                            + DAFTAR_PUBLIKASI.get(i).getPenerbit()+ ","
                            + DAFTAR_PUBLIKASI.get(i).getPenulis()+")";
                    statement.executeUpdate(sql);
                }

                conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public static void bacaTabelPublikasi(){
        try {
            DAFTAR_PUBLIKASI = new ArrayList<Publikasi>();
            String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:xe";
                String userid = "HR";
                String password = "rosi";
                Connection conn;
                OracleDataSource ds;
                ds = new OracleDataSource();
                ds.setURL(jdbcUrl);
                conn = ds.getConnection(userid, password);
                System.out.println("Koneksi is OK");
                Statement statement = conn.createStatement();
//            Statement statement = conn.createStatement();
            String sql = "select * from publikasi";
            ResultSet result = statement.executeQuery(sql);
            while(result.next()){
                Publikasi temp = new Publikasi() {

                    public int HitungDenda(int lamaPinjam) {
                        throw new UnsupportedOperationException("Not supported yet.");
                    }
                };
                try {
                    temp.setIdKoleksi(result.getString("id_koleksi"));
                } catch (Exception ex) {
                    Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
                }
                temp.setJudul(result.getString("judul"));
                DAFTAR_PUBLIKASI.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Perpustakaan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static ArrayList<Anggota> cariAnggota(String nama){
        ArrayList<Anggota> hasil = new ArrayList<Anggota>();
        for (int i = 0; i < DAFTAR_ANGGOTA.size(); i++) {
            Anggota temp = DAFTAR_ANGGOTA.get(i);
            if(temp.getNama().contains(nama)){
                hasil.add(temp);
            }
        }
        return hasil;
    }

   public static void aturTimer(){

   }

}

